# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: main.py
@time: 2021/8/29 13:12
'''
from flask import Flask,render_template,request,jsonify
import re
import requests

def ff(code):
    global title
    try:
        headers = {
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
            'referer':f'https://danjuanapp.com/funding/{code}?channel=1300100141'
        }
        res = requests.get('http://fund.eastmoney.com/js/fundcode_search.js')
        data = res.text
        ff = re.findall('var r = (.*?);', data)
        title = ''
        fb = eval(ff[0])
        for i in fb:
            if code in i:
                title = i[2]
        url = f'https://danjuanapp.com/djapi/fund/nav-growth/{code}?day=30'
        res = requests.get(url,headers = headers)
        data = res.json()
        if data.get('data'):
            result = data.get('data').get('fund_nav_growth')
            times = []
            zf = []
            for i in result:
                time = i.get('date')
                date = time.replace(time.split('-')[0]+'-','')
                z = i.get('gr_per')
                times.append(time)
                zf.append(z)
            return {'time':times,'price':zf,'title':title}
    except Exception as e:
        print(e)
        return None

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('ff.html')

@app.route('/ff')
def fb():
    id = request.args.get('data')
    data = ff(id)
    if data != None:
        print(data)
        return jsonify({'data':data,'success':0})
    else:
        return jsonify({'success':1})

if __name__ == '__main__':
    app.run(port = 5005)

